﻿using UnityEngine;
using System.Collections;
namespace Room
{

    public class RCUIBase : RCComponent
    {
        public UIType type = UIType.None;

        public float ClickRange = 1.2f;


        private GameObject _uiObj;
        protected GameObject uiObj
        {
            get
            {
                if(_uiObj==null)
                {
                    _uiObj = new GameObject(owner.name);
                    _uiObj.transform.SetParent(owner.charactorManager.room.uiManager.transform);
                    _uiObj.transform.localScale = Vector3.one;
                }
                return _uiObj;
            }
        }

        public RCUIBase(RoomCharactor owner)
            :base(owner)
        {
            this.owner = owner;
        }

        public virtual void Click()
        {
            var self = owner.charactorManager.GetSelfCharactor();
            var var0 = new Vector2(self.transform.position.x, self.transform.position.z);
            var var1 = new Vector2(owner.transform.position.x, owner.transform.position.z);
            var dis = Vector2.Distance(var0, var1);
            if (self!=null && dis <= ClickRange)
            {
                OnClick();
            }
        }

        protected virtual void OnClick()
        {

        }

        public virtual void SetInView(bool inView)
        {

        }

        public override void DoStart()
        {
            base.DoStart();
            
        }

        public override void DoStop()
        {
            base.DoStop();
            if(_uiObj!=null)
            {
                GameObject.Destroy(_uiObj);
            }
                
        }
        public override void LateTick()
        {
            //return;
            if (_uiObj != null)
            {
                var pos = owner.transform.position;
                pos.y += owner.GetHeight();
                pos.y += 0.46f;
                pos = CameraTool.Instance.sceneCamera.WorldToScreenPoint(pos);
                pos = owner.charactorManager.room.uiManager.uiCamera.ScreenToWorldPoint(pos);
                pos.z = 0f;
                _uiObj.transform.position = pos;
            }
        }

        #region bubble

        protected UIChatBubble chatBubble;
        public virtual void ShowBubble(string text)
        {
            if(chatBubble == null)
            {
                var prefab = Resources.Load<GameObject>("Prefab/ChatBubble");

                var chatObj = GameObject.Instantiate<GameObject>(prefab);
                chatObj.transform.SetParent(uiObj.transform);
                chatObj.transform.localPosition = Vector3.zero;
                chatObj.transform.localScale = Vector3.one;

                chatBubble = chatObj.GetComponent<UIChatBubble>();
            }
            chatBubble.Show(text);
        }

        #endregion

        #region button
        UIInnerButton btn;

        protected void InitButton(OnClick onClick)
        {
            if (btn == null)
            {
                var prefab = Resources.Load<GameObject>("Prefab/_UIInnerButton");

                var btnObj = GameObject.Instantiate<GameObject>(prefab);
                btnObj.transform.SetParent(uiObj.transform);
                btnObj.transform.localPosition = Vector3.zero;
                btnObj.transform.localScale = Vector3.one;
                btn = btnObj.GetComponent<UIInnerButton>();
            }
            btn.Init(onClick);
        }

        public void SetButtonDepth(int depth)
        {
            btn.GetComponent<UIWidget>().depth = depth;
        }
        public virtual void SetButtonSprite(string spriteName)
        {
            if (btn != null)
            {
                btn.SetSprite(spriteName);
            }
        }
        public virtual void ShowButton()
        {
            if(btn!=null)
            {
                btn.Show();
            }
        }

        public virtual void HideButton()
        {
            if(btn!=null)
            {
                btn.Hide();
            }
        }
        
        #endregion

        #region boxCollider

        public void CreateBoxCollider()
        {
            if (owner.GetComponent<BoxCollider>() == null)
            {
                var collider = owner.gameObject.AddComponent<BoxCollider>();
                collider.center = new Vector3(0, 1, 0);
                collider.size = new Vector3(1, 2, 1);
            }
        }

        #endregion

        public void CreateRigidbody()
        {
            if(owner.GetComponent<Rigidbody>()==null)
            {
                var rigidbody = owner.gameObject.AddComponent<Rigidbody>();
                rigidbody.useGravity = false;
                rigidbody.mass = 1;
                rigidbody.isKinematic = true;
            }
        }

        public virtual void SetColor(Color color)
        {

        }
    }

}

